#include "supplier.h"
#include "ui_supplier.h"
#include "inquiresql.h"
#include "addmodifysupplier.h"
#include "personsupplier.h"
#include <QTableWidgetItem>
#include <QMessageBox>
#include <QKeyEvent>

#include <QDesktopWidget>

Supplier::Supplier(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Supplier)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );

    this->setLayout(ui->MainLayout);
//    this->setFixedWidth(333);
    this->setMinimumSize(900,300);
    setWindowTitle(tr("供货商信息"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_sql = new InquireSql;
    m_addModifySupplier = new AddModifySupplier(this);
    m_addModifySupplier->hide();

    connect(m_addModifySupplier, SIGNAL(displaySupplier()), this, SLOT(slotDisplaySupplier()));
    QHeaderView* vheaderView = ui->tableWidgetSupplier->verticalHeader();
    vheaderView->setHidden(true);

    //设置列宽可拖动
//    ui->tableWidgetSupplier->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetSupplier->verticalHeader()->setResizeMode(QHeaderView::Interactive);
    //设置table的水平滚动条
    ui->tableWidgetSupplier->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    ui->tableWidgetSupplier->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    //设置与文字相匹配的单元格大小
    ui->tableWidgetSupplier->resizeColumnsToContents();
    ui->tableWidgetSupplier->resizeRowsToContents();

//    displaySupplier();
}

Supplier::~Supplier()
{
    delete ui;
}

void Supplier::displaySupplier()
{
    m_list.clear();
    m_list = m_sql->InquireSupplier();
    ui->tableWidgetSupplier->clear();
    ui->tableWidgetSupplier->setRowCount(m_list.count());

    QStringList headers;
    headers << tr("供货商编码")<< tr("厂商名称") << tr("联系人") << tr("联系电话")
            << tr("电子邮箱") << tr("银行帐号") << tr("联系地址") << tr("备注信息");

    ui->tableWidgetSupplier->setColumnCount(8);
    ui->tableWidgetSupplier->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 8; ++i)
    {
        ui->tableWidgetSupplier->horizontalHeaderItem(i)->setFont(QFont("Helvetica"));
        ui->tableWidgetSupplier->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
    //显示供货商信息
    for(int i = 0; i < m_list.count(); ++i)
    {
        QTableWidgetItem *supplierNumber = new QTableWidgetItem(m_list.at(i).get_personID());
        QTableWidgetItem *ownerName = new QTableWidgetItem(m_list.at(i).get_ownerName());
        QTableWidgetItem *contactName = new QTableWidgetItem(m_list.at(i).get_personName());
        QTableWidgetItem *tel = new QTableWidgetItem(m_list.at(i).get_personTel());
        QTableWidgetItem *email = new QTableWidgetItem(m_list.at(i).get_personEmail());
        QTableWidgetItem *supplierBankAccount = new QTableWidgetItem(m_list.at(i).get_supplierBankAccount());
        QTableWidgetItem *address = new QTableWidgetItem(m_list.at(i).get_personAdress());
        QTableWidgetItem *supplierRemark = new QTableWidgetItem(m_list.at(i).get_supplierRemark());

        //设置供货商编号,联系人为居中对齐
        supplierNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        contactName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

        ui->tableWidgetSupplier->setItem(i, 0, supplierNumber);
        ui->tableWidgetSupplier->setItem(i, 1, ownerName);
        ui->tableWidgetSupplier->setItem(i, 2, contactName);
        ui->tableWidgetSupplier->setItem(i, 3, tel);
        ui->tableWidgetSupplier->setItem(i, 4, email);
        ui->tableWidgetSupplier->setItem(i, 5, supplierBankAccount);
        ui->tableWidgetSupplier->setItem(i, 6, address);
        ui->tableWidgetSupplier->setItem(i, 7, supplierRemark);
    }

    ui->tableWidgetSupplier->setCurrentItem(ui->tableWidgetSupplier->item(m_list.count() - 1 , 1));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetSupplier->resizeColumnsToContents();
    ui->tableWidgetSupplier->resizeRowsToContents();
    setTableWidgetColor();

//    this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool);
//    this->move((QApplication::desktop()->width() - this->width())/2,
//               (QApplication::desktop()->height() - this->height())/2);
//    this->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
//    this->setAttribute(Qt::WA_ShowModal, true);
//    this->show();

//    this->setWindowFlags(Qt::Dialog);
//    this->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
//    this->setAttribute(Qt::WA_ShowModal, true);

//    this->move((QApplication::desktop()->width() - this->width())/2,
//               (QApplication::desktop()->height() - this->height())/2);
//    this->show();
}

void Supplier::slotDisplaySupplier()
{
    displaySupplier();
    emit updateComboBoxSupplier();
}

void Supplier::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetSupplier->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetSupplier->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetSupplier->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252,222,156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void Supplier::on_pushButtonNew_clicked()
{
    m_addModifySupplier->slotDisplayAddModifySupplier(addSupplierPage, 0);

    m_addModifySupplier->setWindowFlags(Qt::Dialog);
    m_addModifySupplier->move((QApplication::desktop()->width() - m_addModifySupplier->width())/2,
               (QApplication::desktop()->height() - m_addModifySupplier->height())/2);
    m_addModifySupplier->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_addModifySupplier->setAttribute(Qt::WA_ShowModal, true);
    m_addModifySupplier->resize(364, 433);
    m_addModifySupplier->show();
}

void Supplier::on_pushButtonModity_clicked()
{
    int supplierNumber = m_list.at(ui->tableWidgetSupplier->currentRow()).get_personID().toInt();
    m_addModifySupplier->slotDisplayAddModifySupplier(modifySupplierPage, supplierNumber);

    m_addModifySupplier->setWindowFlags(Qt::Dialog);
    m_addModifySupplier->move((QApplication::desktop()->width() - m_addModifySupplier->width())/2,
               (QApplication::desktop()->height() - m_addModifySupplier->height())/2);
    m_addModifySupplier->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_addModifySupplier->setAttribute(Qt::WA_ShowModal, true);
    m_addModifySupplier->resize(364, 433);
    m_addModifySupplier->show();
}

void Supplier::on_pushButtonDelete_clicked()
{
    if (m_sql->SupplierIsUsed(m_list.at(ui->tableWidgetSupplier->currentRow()).get_personID()))
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n该供货商信息已被使用,无法删除"));
        return;
    }

    switch(QMessageBox::question(this, tr("              ★温馨提示★"),
                          tr("\n您确定要删除当前的记录吗?删除之后将不可恢复"),
                          QMessageBox::No | QMessageBox::Yes ,QMessageBox::No))
    {
        case QMessageBox::Yes:
            m_sql->DeleteSupplier(m_list.at(ui->tableWidgetSupplier->currentRow()).get_personID());
            slotDisplaySupplier();
            break;
        case QMessageBox::No:
            break;
        default:
            break;
    }
}

void Supplier::on_tableWidgetSupplier_itemDoubleClicked(QTableWidgetItem *)
{
    on_pushButtonModity_clicked();
}

void Supplier::keyPressEvent(QKeyEvent *ev)
{
    switch(ev->key())
    {
        case Qt::Key_Escape:
            this->hide();
            break;
    default:
        QWidget::keyPressEvent(ev);
    }
}
